package com.caseProject.puremvc.mediator.view
{
	import com.caseProject.puremvc.mediator.events.GraphicEvent;
	import com.caseProject.puremvc.mediator.events.MenuDiagramListEvent;
	import com.caseProject.puremvc.model.Diagram;
	
	import fl.controls.List;
	import fl.data.DataProvider;
	
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class MenuDiagramListView extends Sprite
	{
		private var list:List;		
		private var dataProv:DataProvider;

		private var nativeMenu:NativeMenu;
		
		public function MenuDiagramListView()
		{
			super();
			
			this.list = new List ();
			this.dataProv = new DataProvider ();
			
			this.list.dataProvider = dataProv;
			
			this.addChild (this.list);
			this.list.labelField = "name";	
			this.list.iconField = "icon";
			
			this.list.addEventListener(Event.CHANGE, onChange);
			
			nativeMenu = new NativeMenu();			
			var del:NativeMenuItem = new NativeMenuItem("Excluir");
			var rename:NativeMenuItem = new NativeMenuItem("Renomear");
			
			nativeMenu.addItem(del);			
			nativeMenu.addItem(rename);			
			
			del.addEventListener(Event.SELECT, onSelectDel);
			rename.addEventListener(Event.SELECT, onSelectRename);
		}
		
		protected function onSelectRename(event:Event):void
		{
			if (this.list.selectedItem != null)
			{
				this.dispatchEvent(new MenuDiagramListEvent(MenuDiagramListEvent.RENAME_DIAGRAM, this.list.selectedItem as Diagram));
			}
		}
		
		protected function onSelectDel(event:Event):void
		{
			this.dispatchEvent(new MenuDiagramListEvent(MenuDiagramListEvent.DELETE_DIAGRAM, this.list.selectedItem as Diagram));
			this.dataProv.removeItem(this.list.selectedItem);						
		}
		
		protected function onChange(event:Event):void
		{
			this.list.contextMenu = nativeMenu;
			this.dispatchEvent(new MenuDiagramListEvent(MenuDiagramListEvent.SELECT_ITEM,this.list.selectedItem as Diagram)); 
		}
		
		public function setLabels(listDiagram:Vector.<Diagram>):void
		{
			for (var i:int = 0; i < listDiagram.length; i++)
			{
				this.dataProv.addItem(listDiagram[i]);
			}			
		}
		
		public function deleteLabels ():void
		{
			this.dataProv.removeAll();
		}
		
		public function addLabel(listDiagram:Vector.<Diagram>):void
		{
			this.dataProv.addItem(listDiagram[listDiagram.length-1]);
		}
		
		override public function set width (num:Number):void
		{
			this.list.width = num;
		}
	}
}